package h0817;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Scanner;

/**
  * @description 污染水域
  * @author 不知名帅哥
  * @date 2024/8/17 16:24
  * @version 1.0
*/
public class PolluteWaters {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] array = Arrays.stream(sc.nextLine().split(",")).mapToInt(Integer::parseInt).toArray();
        int m = (int) Math.sqrt(array.length);
        int[][] matrix = new int[m][m];//
        int k=0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < m; j++) {
                matrix[i][j]=array[k];
                k++;
            }
        }
        BFS(matrix);
    }

    private static void BFS(int[][] matrix) {

        int m = matrix.length;
        int ans=0;
        //定义一个队列
        LinkedList<int[]> queue = new LinkedList<>();
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < m; j++) {
                if (matrix[i][j]==1){
                    queue.addLast(new int[]{i,j});
                }
            }
        }
        //方向
        int[][] direction = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                int[] temp = queue.removeFirst();
                for (int j = 0; j < direction.length; j++) {
                    int newI = temp[0] + direction[j][0];
                    int newJ = temp[1] + direction[j][1];
                    if (newI >= 0 && newI < m && newJ >= 0 && newJ < m && matrix[newI][newJ] != 1) {
                        matrix[newI][newJ] = 1;
                        queue.addLast(new int[]{newI, newJ});
                    }
                }
            }
            ans++;
        }
        //最后把最后的节点poll出去会多算一秒。
        System.out.println(ans-1);
    }
}
